package com.syni.mdd.yhd.common.all.controller.protect.business;

/**
 * @className BmsBusinessController
 * @description TOO
 * @Author cfx
 * @DATE 2019/4/15 14:25
 * @VERSION 1.0
 **/

import com.syni.mdd.sjb.common.component.utils.common.annotations.CheckParams;
import com.syni.mdd.sjb.common.component.utils.common.annotations.EntranceCheck;
import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.constants.ResultCode;
import com.syni.mdd.yhd.common.all.dto.requestDto.business.BusinessReDto;
import com.syni.mdd.yhd.common.all.entity.BmsBusiness;
import com.syni.mdd.yhd.common.all.entity.bo.user.CheckUserBo;
import com.syni.mdd.yhd.common.all.service.BmsBusinessService;
import com.syni.mdd.yhd.common.all.service.BmsCommonService;
import com.syni.mdd.sjb.common.component.utils.common.utils.RequestHeaderData;
import com.syni.mdd.yhd.common.all.utils.RequestParamUtils;
import com.syni.mdd.yhd.common.all.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/business")
public class BmsBusinessController {
    @Autowired
    BmsBusinessService bmsBusinessService;

    @PostMapping("/getBusinessIndex")
    @CheckParams(params = {"business_id"})
    public Map<String,Object> getBusinessIndex(@RequestBody Map<String,Object> paramsMap){
        CheckUserBo checkUserBo = RequestParamUtils.checkUserBo();
        //默认使用设备标识
        String userId = null;
        if(checkUserBo.getCheckStatus().equals(ResultCode.CODE_NORMAL)){
            userId = checkUserBo.getUserId();
        }
        Integer businessId = Integer.parseInt(paramsMap.get("business_id").toString());
        String latitude = null;
        String longitude = null;
        if(paramsMap.containsKey("latitude")){
            latitude = paramsMap.get("latitude").toString();
        }
        if(paramsMap.containsKey("longitude")){
            longitude = paramsMap.get("longitude").toString();
        }
        Integer pageSize = 9;
        if(paramsMap.containsKey("page_size") && StringUtils.checkNullAndEmpty(paramsMap.get("page_size"))){
            pageSize = Integer.parseInt(paramsMap.get("page_size").toString());
        }
        return bmsBusinessService.findBussinessIndex(userId,businessId,latitude,longitude,pageSize);
    }

    @PostMapping("/getBusinessVideo")
    @CheckParams(params = {"business_id","page_num","page_size"})
    public Map<String,Object> getBusinessVideo(@RequestBody Map<String,Object> paramsMap){
        Integer businessId = Integer.parseInt(paramsMap.get("business_id").toString());
        Map<String,Object> checkRet = RequestParamUtils.checkAuthor(paramsMap);
        String userId = null;
        if(checkRet.get("status").toString().equals(ResultCode.CODE_NORMAL)){
            userId = checkRet.get("userIdentify").toString();
        }
        String latitude = null;
        String longitude = null;
        if(paramsMap.containsKey("latitude")){
            latitude = paramsMap.get("latitude").toString();
        }
        if(paramsMap.containsKey("longitude")){
            longitude = paramsMap.get("longitude").toString();
        }
        Integer pageNum = Integer.parseInt(paramsMap.get("page_num").toString());
        Integer pageSize = Integer.parseInt(paramsMap.get("page_size").toString());
        return bmsBusinessService.findBusinessVideo(userId,businessId,pageNum,pageSize,latitude,longitude);
    }

    @PostMapping("/searchBusinessToClaim")
    @CheckParams(params = {"addr_city","search_text","page_num","page_size"})
    public Map<String,Object> searchBusinessToClaim(@RequestBody Map<String,Object> paramsMap){
        String addrCity = paramsMap.get("addr_city").toString();
        String searchText = paramsMap.get("search_text").toString();
        Integer pageNum = Integer.parseInt(paramsMap.get("page_num").toString());
        Integer pageSize = Integer.parseInt(paramsMap.get("page_size").toString());
        return bmsBusinessService.findBusinessToFront(addrCity,searchText,pageNum,pageSize);
    }

    @PostMapping("/focusOnBusiness")
    @CheckParams(checkAuth = true,params = {"business_id"})
    public Map<String,Object> focusOnBusiness(@RequestBody Map<String,Object> paramsMap){
        Integer businessId = Integer.parseInt(paramsMap.get("business_id").toString());
        Integer userId = Integer.parseInt(paramsMap.get("userIdentify").toString());
        Integer isMandatory = null;
        if(paramsMap.containsKey("isMandatory") && StringUtils.checkNullAndEmpty(paramsMap.get("isMandatory"))){
            isMandatory = Constants.COMMON_FLAG_TRUE_INT;
        }
        return bmsBusinessService.focusOnBusiness(userId,businessId,isMandatory);
    }

    @PostMapping("/searchBusinessByContent")
    @CheckParams(params = {"business_name","page_num","page_size"})
    public Map<String,Object> searchBusinessByContent(@RequestBody Map<String,Object> paramsMap){
        String businessName = paramsMap.get("business_name").toString();
        Integer pageNum = Integer.parseInt(paramsMap.get("page_num").toString());
        Integer pageSize = Integer.parseInt(paramsMap.get("page_size").toString());
        String addrCity = paramsMap.get("addrCity").toString();
        String addrDistrict = paramsMap.get("addrDistrict").toString();
        return bmsBusinessService.findBusinessToReleaseVideo(businessName,addrCity,addrDistrict,pageNum,pageSize);
    }

    /** 
     * @Description: 推荐热门店铺
     * @Author: cfx 
     * @Date: 2019/8/9 
     */
    @PostMapping("/recommendHotBusiness")
    @EntranceCheck
    public Map<String,Object> recommendHotBusiness(@RequestBody BusinessReDto businessReDto){
        return bmsBusinessService.recommendHotBusiness(businessReDto);
    }


    @GetMapping("/doBu")
    public void doBu() {
        Integer pageSize = 100;
        Integer pageNum = 1;
        for (; ; ) {
            Pageable pageable = BmsCommonService.getPageable(pageNum,pageSize);
            List<BmsBusiness> bmsBusinessList = bmsBusinessService.getBaseDao().findByIsDeleteAndIsUnloadAndTagsLike(Constants.COMMON_FLAG_FALSE_INT,
                    Constants.COMMON_FLAG_FALSE_INT,pageable,"%首%");
            if(bmsBusinessList != null && bmsBusinessList.size() > 0){
                for(BmsBusiness business : bmsBusinessList){
                    List<String> labelsStr = new LinkedList<>();
                    String[] labels = business.getTags().split(",");
                    if(labels.length > 0){
                        for(String label : labels){
                            if(label.indexOf("首") == -1){
                                labelsStr.add(label);
                            }
                        }
                        business.setTags(StringUtils.join(labelsStr,","));
                        System.out.println("标签数据：" + business.getLabel());
                    }
                }
                bmsBusinessService.getBaseDao().saveAll(bmsBusinessList);
            }else {
                break;
            }
            pageNum++;
        }
    }

}
